﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using mybolg.Models;
using System.Web.Mvc;

namespace mybolg.DAL
{
   
    public  class Blog_MainPage
    {
        Models.BlogEntities11 DBblog = new BlogEntities11();
        //按页查询出十条记录  按时间排序 以及总数
      public  Dictionary<string, object> getShuoShuoByPageAndNumber(int Page, int Number)
        {
            int maxNumber = getShuoShuoCount();
            int maxPage = maxNumber / Number;
            int lastNumber = maxNumber % Number;
            if (lastNumber > 0) { maxPage += 1; }
            string Querystr = "select Top " + Number + " * from (select top " + Page * Number + " * from dbo.DMP_Bolg_Shuoshuo order by Time desc)ss  order by Time ";
            if (lastNumber > 0&&maxPage == Page)
            {
                Querystr = "select Top " + lastNumber + " * from (select top " + Page * Number + " * from dbo.DMP_Bolg_Shuoshuo order by Time desc)ss  order by Time ";
            }
            var ssList = DBblog.Database.SqlQuery<DMP_Bolg_Shuoshuo>(Querystr).ToList();
            //形成返回数据
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("maxPage", maxPage);
            dic.Add("maxNumber", maxNumber);
            dic.Add("ssList", ssList);
         
            return dic;
        }

        public  int getShuoShuoCount()
        {
            string Querystr = "select COUNT(*) as count  from dbo.DMP_Bolg_Shuoshuo";
            var Count = DBblog.Database.SqlQuery<int>(Querystr).ToList()[0];
           return Count;
        }

        public string DBstrShuoShuoForPageRequestTime()
        {
            //DateTime PageRequertTime=Session["HomeIndexRequestTime"] as DateTime;
            string str = "";

            return str;
        
        
        }

        class SScount
        {
          public  int count;
        }




        public string shuoshuoDetail_add(string MessageType, string Name, string MessageProfile, string MessageDetail)
        {
           
            DMP_Bolg_Shuoshuo shuoshuo = new DMP_Bolg_Shuoshuo();
            shuoshuo.id = Guid.NewGuid().ToString();
            shuoshuo.MessageProfile = MessageProfile;
            shuoshuo.MessagesAddress = "";
            shuoshuo.MessageType = MessageType;
            shuoshuo.Name =Name;
            shuoshuo.PageViewNumber =0;
            shuoshuo.PointNumber =0;
            shuoshuo.shareNumber =0;
            shuoshuo.Time =DateTime.Now;
            shuoshuo.Time_Update = DateTime.Now;
            shuoshuo.CommentNumber =0;
         

            //保存详情
            DMP_Bolg_ShuoShuoDetail ssDetail = new DMP_Bolg_ShuoShuoDetail();
            ssDetail.Detail = MessageDetail;
            ssDetail.id = Guid.NewGuid().ToString(); ;
            ssDetail.ShuoShuoId = shuoshuo.id;

            shuoshuo.MessagesAddress = "/shuoshuo/details/"+ ssDetail.id;
            DBblog.DMP_Bolg_Shuoshuo.Add(shuoshuo);
            DBblog.DMP_Bolg_ShuoShuoDetail.Add(ssDetail);
            DBblog.SaveChanges();

            return "true";
        }
        /// <summary>
        /// 编辑说说
        /// </summary>
        /// <param name="id"></param>
        /// <param name="MessageType"></param>
        /// <param name="Name"></param>
        /// <param name="MessageProfile"></param>
        /// <param name="MessageDetail"></param>
        /// <returns></returns>
        public string shuoshuoDetail_edit(string id, string MessageType, string Name, string MessageProfile, string MessageDetail)
        {
            try
            {
                var ss = DBblog.DMP_Bolg_Shuoshuo.Find(id);
                ss.Time_Update = DateTime.Now;
                ss.Name = Name;
                ss.MessageType = MessageType;
                ss.MessageProfile = MessageProfile;
                DBblog.Entry(ss).State = System.Data.EntityState.Modified;
                //数据编辑
                string sql = @"UPDATE [Blog].[dbo].[DMP_Bolg_ShuoShuoDetail] 
                    SET Detail = '"+ MessageDetail + "'  WHERE ShuoShuoId = '"+ss.id+"'";
                DBblog.Database.ExecuteSqlCommand(sql);
                DBblog.SaveChanges();
            }
            catch (Exception e) { return "false"; }

            return "true";
        }

        /// <summary>
        /// 更改说说
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string shuoshuoDetail_delete(string id)
        {
            try
            {
                var ss = DBblog.DMP_Bolg_Shuoshuo.Find(id);
                ss.IsDelete = 1;
                ss.Time_Delete = DateTime.Now;
                DBblog.Entry(ss).State = System.Data.EntityState.Modified;
                DBblog.SaveChanges();
            }
            catch (Exception e) { return "false"; }
            return "true";
        }


    }
}